Add RegisterNatives overload for raw function pointers#1391
Merged
jonathanpeppers merged 4 commits intomainfrom Mar 19, 2026
Merged
Add RegisterNatives overload for raw function pointers#1391jonathanpeppers merged 4 commits intomainfrom
jonathanpeppers merged 4 commits intomainfrom
Conversation
Add JniNativeMethod struct matching JNI's JNINativeMethod layout (byte* Name, byte* Signature, IntPtr FunctionPointer) and a new RegisterNatives(JniObjectReference, ReadOnlySpan<JniNativeMethod>) overload. Calls the JNI RegisterNatives function pointer directly via the function table, bypassing delegate marshaling entirely. Zero allocations in the entire path. Add end-to-end test with a dedicated Java class (RegisterNativesTestType) and an [UnmanagedCallersOnly] native callback that is registered via the new API, then called from Java to verify the result. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Member
jonathanpeppers
left a comment
There was a problem hiding this comment.
##[error]src\Java.Interop\Java.Interop\JniEnvironment.Types.cs(288,99): Error CS1061: 'JniEnvironmentInfo' does not contain a definition for 'Invoker' and no accessible extension method 'Invoker' accepting a first argument of type 'JniEnvironmentInfo' could be found (are you missing a using directive or an assembly reference?)
D:\a\1\s\src\Java.Interop\Java.Interop\JniEnvironment.Types.cs(288,99): error CS1061: 'JniEnvironmentInfo' does not contain a definition for 'Invoker' and no accessible extension method 'Invoker' accepting a first argument of type 'JniEnvironmentInfo' could be found (are you missing a using directive or an assembly reference?) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
##[error]src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(30,23): Error RS0016: Symbol 'implicit constructor for 'JniNativeMethod'' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
D:\a\1\s\src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(30,23): error RS0016: Symbol 'implicit constructor for 'JniNativeMethod'' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
##[error]src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(36,10): Error RS0016: Symbol 'JniNativeMethod' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
D:\a\1\s\src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(36,10): error RS0016: Symbol 'JniNativeMethod' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
##[error]src\Java.Interop\PublicAPI.Unshipped.txt(16,1): Error RS0017: Symbol 'Java.Interop.JniNativeMethod.JniNativeMethod(byte* name, byte* signature, System.IntPtr functionPointer) -> void' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
D:\a\1\s\src\Java.Interop\PublicAPI.Unshipped.txt(16,1): error RS0017: Symbol 'Java.Interop.JniNativeMethod.JniNativeMethod(byte* name, byte* signature, System.IntPtr functionPointer) -> void' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
- Fix CS1061: Use JNIEnv function table directly via (*((JNIEnv**)env)) instead of info.Invoker which doesn't exist in the function pointers compilation path - Fix RS0016: Add implicit parameterless constructor to PublicAPI - Fix RS0016/RS0017: Use nint instead of System.IntPtr in PublicAPI (matches .NET 10 representation) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The Java class was not listed in the JavaInteropTestJar item group, causing ClassNotFoundException at runtime. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jonathanpeppers
approved these changes
Mar 19, 2026
jonathanpeppers
added a commit
to dotnet/android
that referenced
this pull request
Mar 24, 2026
…avaConvert (#10967) Fixes: #10791 ## Summary Adds the runtime-side support for the trimmable typemap: type resolution, peer creation, native method registration, and AOT-safe collection marshaling. All behind `RuntimeFeature.TrimmableTypeMap` (defaults to `false`). ### New runtime types - **`JavaPeerProxy` / `JavaPeerProxy<T>`** — AOT-safe proxy attribute base. Generated proxy types extend this and provide `CreateInstance()` for peer creation and `GetContainerFactory()` for collection marshaling. - **`IAndroidCallableWrapper`** — `RegisterNatives(JniType)` interface for ACW proxy types to register JNI native methods. - **`JavaPeerContainerFactory`** — AOT-safe factories for arrays, lists, collections, and dictionaries without `MakeGenericType()`. - **`TrimmableTypeMap`** — Central class owning the `TypeMapping` dictionary. Encapsulates all proxy attribute access: peer creation, invoker resolution, container factories, and native method registration bootstrap. - **`TrimmableTypeMapTypeManager`** — `JniTypeManager` subclass delegating type lookups to `TrimmableTypeMap`. `RegisterNativeMembers` throws `UnreachableException` (JCW static blocks handle registration). ### Type resolution - `GetProxyForManagedType(Type)` resolves managed type → JNI name via `IJniNameProviderAttribute` (shared interface for `[Register]` and `[JniTypeSignature]`) → TypeMap dictionary → proxy. Results cached in `ConcurrentDictionary`. - `TryGetJniNameForType` shared between `TrimmableTypeMap` and `TrimmableTypeMapTypeManager`. - `ActivateInstance` uses JNI `GetObjectClass` → `GetJniTypeNameFromClass` → TypeMap lookup for constructor activation (proxy types have self-applied attribute, not the target type). ### TypeMap dictionary initialization - The runtime's `GetOrCreateExternalTypeMapping<Java.Lang.Object>()` handles assembly resolution automatically via the `System.Runtime.InteropServices.TypeMappingEntryAssembly` runtimeconfig property (set in `Trimmable.targets`). No manual `Assembly.Load` pre-loading is needed — the runtime walks `TypeMapAssemblyTargetAttribute` attributes from the entry assembly recursively. ### Native interop refactoring - **`RegisterJniNatives` passed via init args** — `Initialize` sets `args->registerJniNativesFn` (null when `TrimmableTypeMap=true`). Eliminates the `create_delegate` call for `RegisterJniNatives`, letting the trimmer remove it cleanly in the trimmable path. - **C++ `registerNatives` stub** — `Host::Java_mono_android_Runtime_registerNatives` no-op for the trimmable path (managed code handles registration via `Initialize`). - **Null guard** on `jnienv_register_jni_natives` call site. - `Initialize` is now the **single** `create_delegate` entry point from native code. ### RegisterNatives bootstrap - `mono.android.Runtime.registerNatives(Class)` Java native method added - `TrimmableTypeMap.Initialize()` registers the JNI callback during init (behind explicit `if (RuntimeFeature.TrimmableTypeMap)` guard for trimmer compatibility) - When Java loads a JCW class, `OnRegisterNatives` resolves the proxy and calls `IAndroidCallableWrapper.RegisterNatives()` to bind UCO function pointers ### AOT-safe JavaConvert - `JavaConvert.GetJniHandleConverter()` uses `JavaPeerContainerFactory` for `IList`, `ICollection`, `IDictionary` - `JNIEnv.ArrayCreateInstance()` uses factory path ### Peer creation - `JavaMarshalValueManager` (renamed from `ManagedValueManager`) overrides `CreatePeer` for the trimmable path — calls `proxy.CreateInstance()` directly, bypassing `GetUninitializedObject` - `GetSimpleReferences` walks base type chain for managed-only subclasses without `[Register]` ### Wiring - `RuntimeFeature.TrimmableTypeMap` feature switch with ILLink substitutions - `JNIEnvInit` (CoreCLR) and `JavaInteropRuntime` + `JreRuntime` (NativeAOT) create the new managers when the feature is on - `JavaMarshalValueManager` (renamed from `ManagedValueManager`) gets proxy-based peer creation in `TryConstructPeer` ### Dependencies - dotnet/java-interop#1391 (RegisterNatives with raw function pointers — follow-up optimization) - Manifest generator: #10991 - Build pipeline: #10980 ### Test coverage - 255/255 generator tests pass - End-to-end HelloWorld app runs on device with Activity, layout XML, FindViewById\<Button\>, Click events - Runtime tests require device integration (tracked by #10793) ### Trimmer size regression prevention - Add ILLink substitutions for `IsMonoRuntime` and `IsCoreClrRuntime` — enables trimmer to eliminate runtime-specific branches - `JavaMarshalValueManager`: remove `TrimmableTypeMap?` field, use `TrimmableTypeMap.Instance` singleton - `TrimmableTypeMap`: private ctor, `Initialize()` static method, `Instance` throws if not initialized - `TrimmableTypeMapTypeManager`: no constructor parameter, uses singleton - No `TrimmableTypeMap` type references outside feature guards — clean trimming for MonoVM/NativeAOT Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Jonathan Peppers <jonathan.peppers@microsoft.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add JniNativeMethod struct matching JNI's JNINativeMethod layout
(byte* Name, byte* Signature, IntPtr FunctionPointer)and a newRegisterNatives(JniObjectReference, ReadOnlySpan<JniNativeMethod>)overload.Calls the JNI RegisterNatives function pointer directly via the function table, bypassing delegate marshaling entirely. Zero allocations in the entire path.
Add end-to-end test with a dedicated Java class (RegisterNativesTestType) and an
[UnmanagedCallersOnly]native callback that is registered via the new API, then called from Java to verify the result.